/*!
 * 文件名称： bsp_can.c
 * 描    述： bsp can interface
 * 版本：     2023-12-03, V1.0
*/

/*
* GD32F303东方红开发板V1.0
* 淘宝商城：   https://juwo.taobao.com
* 技术交流：   https://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "driver_can.h"
#include "bsp_can.h"

can_receive_message_struct can0_receive_message_fifo0;
FlagStatus can0_receive_fifo0_flag;
can_receive_message_struct can0_receive_message_fifo1;
FlagStatus can0_receive_fifo1_flag;

typdef_can_general BSP_CAN = 
{
	.can_port = CAN0,
	.rcu_can = RCU_CAN0,		
	.rcu_IO_port = RCU_GPIOB,
	.IO_port = GPIOB,					
	.pin_tx = GPIO_PIN_9,							
	.pin_rx = GPIO_PIN_8,						
	.can_remap = GPIO_CAN_PARTIAL_REMAP,
	.gpio_speed = GPIO_OSPEED_50MHZ	,		
	.can_rx_use_interrupt = SET
};

can_trasnmit_message_struct bsp_can_transmit_message_1 = {
	.tx_sfid = 0x300,
	.tx_efid = 0x00,
	.tx_ft = CAN_FT_DATA,
	.tx_ff = CAN_FF_STANDARD,
	.tx_dlen = 2,
	.tx_data[0] = 0x55,
	.tx_data[1] = 0xAA,
};

can_trasnmit_message_struct bsp_can_transmit_message_2 = {
	.tx_sfid = 0x500,
	.tx_efid = 0x00,
	.tx_ft = CAN_FT_DATA,
	.tx_ff = CAN_FF_STANDARD,
	.tx_dlen = 2,
	.tx_data[0] = 0x01,
	.tx_data[1] = 0x02,
};

can_trasnmit_message_struct bsp_can_transmit_message_3 = {
	.tx_sfid = 0x400,
	.tx_efid = 0x00,
	.tx_ft = CAN_FT_DATA,
	.tx_ff = CAN_FF_STANDARD,
	.tx_dlen = 2,
	.tx_data[0] = 0x02,
	.tx_data[1] = 0x01,
};

/*!
* 说明     BSP层CAN配置函数
* 输入     typdef_can_general结构体
* 返回值   无
*/
void bsp_can_config(typdef_can_general can_general)
{
	driver_can_config(can_general);
}


/*!
* 说明     BSP层CAN发送函数
* 输入[1]  typdef_can_general结构体
* 输入[2]  can_trasnmit_message_struct结构体指针
* 返回值   无
*/
void bsp_can_transmit(typdef_can_general can_general,can_trasnmit_message_struct *transmit_message)
{
	driver_can_transmit(can_general,transmit_message);
}

/*!
* 说明     CAN FIFO0接收中断函数
* 输入     无
* 返回值   无
*/
void can0_rx0_interrupt_handler(void)
{
	    /* check the receive message */
    can_message_receive(CAN0, CAN_FIFO0, &can0_receive_message_fifo0);
    
    if((0x300 == can0_receive_message_fifo0.rx_sfid)&&(CAN_FF_STANDARD == can0_receive_message_fifo0.rx_ff)&&(2 == can0_receive_message_fifo0.rx_dlen)){
        can0_receive_fifo0_flag = SET; 
    }else{
        can0_receive_fifo0_flag = RESET; 
    }
}

/*!
* 说明     CAN FIFO1接收中断函数
* 输入     无
* 返回值   无
*/
void can0_rx1_interrupt_handler(void)
{
	    /* check the receive message */
    can_message_receive(CAN0, CAN_FIFO1, &can0_receive_message_fifo1);
    
    if((0x500 == can0_receive_message_fifo1.rx_sfid)&&(CAN_FF_STANDARD == can0_receive_message_fifo1.rx_ff)&&(2 == can0_receive_message_fifo1.rx_dlen)){
        can0_receive_fifo1_flag = SET; 
    }else{
        can0_receive_fifo1_flag = RESET; 
    }
}


